package com.smarthome.common.tools;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by xhyan_000 on 2015/12/15.
 */
public class RegexUtil {

    /**
     * 判断是否是正确的IP地址
     *
     * @param ip
     * @return boolean true,通过，false，没通过
     */
    public static boolean isIp(String ip) {
        if (null == ip || "".equals(ip))
            return false;
        String regex = "((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)";
        return ip.matches(regex);
    }

    /**
     * 判断是否是正确的邮箱地址
     *
     * @param email
     * @return boolean true,通过，false，没通过
     */
    public static boolean isEmail(String email) {
        if (null == email || "".equals(email))
            return false;
        String regex = "[a-z0-9]([a-z0-9]*[-_\\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\\.][a-z]{2,3}([\\.][a-z]{2})?";
        return email.matches(regex);
    }

    /**
     * 判断是否是正确的银行卡号
     *
     * @param cardNo
     * @return boolean true,通过，false，没通过
     */
    public static boolean isBankCard(String cardNo) {
        if (null == cardNo || "".equals(cardNo))
            return false;
        String regex = "([0-9]){15,19}";
        return cardNo.matches(regex);
    }

    /**
     * 判断是否是正确的图片
     *
     * @param image
     * @return boolean true,通过，false，没通过
     */
    public static boolean isImage(String image) {
        if (null == image || "".equals(image))
            return false;
        String regex = "\\.(jpg|png|jpeg)+$";
        return image.matches(regex);
    }

    /**
     * 判断是否是正确的手机验证码
     *
     * @param verifyCode
     * @return boolean true,通过，false，没通过
     */
    public static boolean isVerifyCode(String verifyCode) {
        if (null == verifyCode || "".equals(verifyCode))
            return false;
        String regex = "([0-9]){6}";
        return verifyCode.matches(regex);
    }

    /**
     * 判断是否是正确的姓名
     *
     * @param name
     * @return boolean true,通过，false，没通过
     */
    public static boolean isName(String name) {
        if (null == name || "".equals(name))
            return false;
        String regex = "^[\\u4E00-\\u9FA5•]{2,12}$";
        return name.matches(regex);
    }

    /**
     * 判断是否是正确的姓名
     *
     * @param companyName
     * @return boolean true,通过，false，没通过
     */
    public static boolean isCompanyName(String companyName) {
        if (null == companyName || "".equals(companyName))
            return false;
        String regex = "^[\\u4E00-\\u9FA5]{4,50}$";
        return companyName.matches(regex);
    }

    /**
     * 判断是否含有中文，仅适合中国汉字，不包括标点
     *
     * @param text
     * @return boolean true,通过，false，没通过
     */
    public static boolean isChinese(String text) {
        if (null == text || "".equals(text))
            return false;
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(text);
        return m.find();
    }

    /**
     * 判断是否是金额
     *
     * @param money 数字
     * @return boolean true,通过，false，没通过
     */
    public static boolean isMoney(String money) {
        if (null == money || "".equals(money))
            return false;
        String regex = "^[1-9]\\d*|0$";
        return money.matches(regex);
    }

    /**
     * 判断是否正整数
     *
     * @param number 数字
     * @return boolean true,通过，false，没通过
     */
    public static boolean isNumber(String number) {
        if (null == number || "".equals(number))
            return false;
        String regex = "";
        return number.matches(regex);
    }

    /**
     * 判断几位小数(正数)
     *
     * @param decimal 数字
     * @param count 小数位数
     * @return boolean true,通过，false，没通过
     */
    public static boolean isDecimal(String decimal, int count) {
        if (null == decimal || "".equals(decimal))
            return false;
        String regex = "^(-)?(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){" + count + "})?$";
        return decimal.matches(regex);
    }

    /**
     * 判断是否是手机号码
     *
     * @param mobile 手机号码
     * @return boolean true,通过，false，没通过
     */
    public static boolean isMobile(String mobile) {
        if (null == mobile || "".equals(mobile))
            return false;
        String regex = "^1(3[0-9]\\d|47\\d|5[0-35-9]\\d|7[67]\\d|8[0-35-9]\\d|70[059])\\d{7}$";
        return mobile.matches(regex);
    }

    /**
     * 判断是否是身份证号
     *
     * @param credentailNo 身份证号
     * @return boolean true,通过，false，没通过
     */
    public static boolean isCredentailNo(String credentailNo) {
        if (null == credentailNo || "".equals(credentailNo))
            return false;
        IdCardValidator validator = new IdCardValidator();
        // return (validator.isIdcard(credentailNo) &&
        // validator.isValidatedAllIdcard(credentailNo));
        return validator.isIdcard(credentailNo);
    }

    /**
     * 判断是否营业执照编号
     *
     * @param bizCode 营业执照编号
     * @return boolean true,通过，false，没通过
     */
    public static boolean isBizCode(String bizCode) {
        if (null == bizCode || "".equals(bizCode))
            return false;
        String regex = "\\d{15}";
        // return bizCode.matches(regex) &&
        // BizCodeValidator.isValidBizCode(bizCode);
        return bizCode.matches(regex);
    }

    /**
     * 判断是否是登录密码
     *
     * @param pwd 密码
     * @return boolean true,通过，false，没通过
     */
    public static boolean isLoginPwd(String pwd) {
        if (null == pwd || "".equals(pwd))
            return false;
        String regex = "([0-9a-zA-Z]){8,20}";
        return pwd.matches(regex);
    }

    /**
     * 判断是否是网址
     *
     * @param url 网址
     * @return boolean true,通过，false，没通过
     */
    public static boolean isUrl(String url) {
        if (null == url || "".equals(url))
            return false;
        String regex = "(\\\\w+)://([^/:]+)(:\\\\d*)?([^#\\\\s]*)";
        return url.matches(regex);
    }

    /**
     * 判断是否含有特殊字符
     *
     * @param text
     * @return boolean true,通过，false，没通过
     */
    public static boolean hasSpecialChar(String text) {
        if (null == text || "".equals(text))
            return false;
        if (text.replaceAll("[a-z]*[A-Z]*\\d*-*_*\\s*", "").length() == 0) {
            // 如果不包含特殊字符
            return true;
        }
        return false;
    }

    /**
     * 适应CJK（中日韩）字符集，部分中日韩的字是一样的
     */
    public static boolean isChinese2(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }

    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
            return true;
        }
        return false;
    }
}
